package com.smartlight.godlight.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.smartlight.godlight.R;
import com.smartlight.godlight.SelectGod;
import com.smartlight.godlight.constant.GodlApi;
import com.smartlight.godlight.listener.OnItemClickListener;
import com.smartlight.godlight.model.Buddha;

import java.util.List;

public class BuddhaAdapter extends RecyclerView.Adapter<BuddhaAdapter.MyViewHolder> {
    private List<Buddha> mData; // 假设这是你的数据源
    Context context;
    private static OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listenser) {
        onItemClickListener = listenser;
    }
    public BuddhaAdapter(Context context,List<Buddha> data) {
        this.context = context;
        this.mData = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建ViewHolder的实例，传入布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buddha_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        // 佛像名称
        holder.fruitName.setText(mData.get(position).getName());
        // 将数据绑定到ViewHolder上
        RequestOptions requestOptions = new RequestOptions()
                .centerCrop() // 可以根据需要调整，例如：fitCenter, centerCrop等
                .placeholder(R.drawable.guanyin) // 加载过程中的占位图
                .error(R.drawable.guanyin); // 加载失败时显示的图片
        // 设置图片
        Glide.with(context)
                .load(GodlApi.BASEURL + mData.get(position).getImg())
                .error(R.drawable.bg)
                .apply(RequestOptions.circleCropTransform()) // 应用圆形裁剪
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)) // 设置缓存策略，这里是缓存源资源和结果图像到磁盘缓存中
                .timeout(5000)
                .into(holder.fruitImage);
        // 佛像图片
        if (position == selectedPosition) {
            holder.fruitImage.setSelected(true);
        } else {
            holder.fruitImage.setSelected(false);
        }

        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.fruitImage.setSelected(!holder.fruitImage.isSelected());
                setSelectedPosition(position);
                onItemClickListener.onItemClick(position);
            }
        });
    }

    private int selectedPosition = -1; // 初始化为无效值

    public void setSelectedPosition(int position) {
        selectedPosition = position;
        notifyDataSetChanged(); // 通知adapter数据已改变，重新绘制所有项
    }

    @Override
    public int getItemCount() {
        // 返回数据集的大小
        return mData.size();
    }

    static class  MyViewHolder extends RecyclerView.ViewHolder {
        ImageView fruitImage;
        TextView fruitName;

        public MyViewHolder(View itemView) {
            super(itemView);
            fruitImage = itemView.findViewById(R.id.fruit_image);
            fruitName = itemView.findViewById(R.id.fruit_name);

            // 监听item点击事件
            // item点击监听
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }
}
